<template>
  <div>
    <nav-bar :title="title" :isShow="false"></nav-bar>
    <van-form @submit="onSubmit">
      <van-field
        v-model="name"
        name="name"
        label="姓名"
        placeholder="姓名"
        :rules="[{ required: true, message: '请填写姓名' }]"
      />
      <van-field
        v-model="telephone"
        name="telephone"
        label="手机号"
        placeholder="手机号"
      />
      <van-field
        v-model="agency"
        name="agency"
        label="授权状态"
        placeholder="授权状态"
        readonly
      />
      <div style="margin: 16px;">
        <van-button v-if="member_role === '0'"  round block type="info" native-type="submit">授权提交</van-button>
         <go-home-button v-else></go-home-button>
      </div>
    </van-form>
   
  </div>
</template>

<script>
import NavBar from '@/components/NavBar'
import GoHomeButton from '@/components/GoHomeButton'
import { card_agency_info, card_agency_update } from '@/api/service'
import { get_role } from '@/api/role'
import { subscribe_url } from '@/utils/const'

export default {
  components: {
    NavBar,
    GoHomeButton
  },
  props: {
    user_id: String
  },
  data() {
    return {
      title: this.$route.meta.title || "",
      member_role: sessionStorage.getItem("member_role", "0"),
      id: 0,
      openid: "",
      office_openid: "",
      name: "",
      telephone: "",
      is_agency: 0,
    };
  },
  computed:{
    agency(){
      if(this.is_agency === 1){
        return '已经授权'
      }else{
        return '未授权'
      }
    }
  },
  methods: {
    onSubmit(values) {
      const openid = localStorage.getItem("openid")
           
      values["openid"] = openid
      values["is_agency"] = 1
      console.log("onSubmit:", values)

      values["id"] = this.id
      this.UpdateCardAgencyInfo(values)
    },

    UpdateCardAgencyInfo(userInfo){
      card_agency_update(userInfo).then(res=>{
        console.log("UpdateCardAgencyInfo:", res)
        if(res.data.id){
          const openid = localStorage.getItem("openid");
          get_role(openid).then(res => {
            if(!res.subscribe){
              window.location.href = subscribe_url;
            }else{
              this.$router.push('/card_agency');
            }
          }).catch(error => {
            console.log(error);
            this.$router.push('/card_agency');
          })
          
        }
      }).catch(error => {
        if (error.response) {
          console.log(error.response.data);
        } else if (error.request) {
          console.log(error.request);
        } else {
          console.log('Error', error.message);
        }
        console.log(error.config);
      })
    },

    getCardAgencyInfo(){
      const openid = localStorage.getItem('openid')
      if(openid){
        card_agency_info(this.user_id).then(res => {
          console.log("getCardAgencyInfo:",res)
          this.id = res.data.id
          this.name = res.data.name
          this.telephone = res.data.telephone
          this.office_openid = res.data.office_openid
          this.is_agency = res.data.is_agency
        }).catch(error => {
            if (error.response) {
              console.log("b:", error.response.status);
            } else if (error.request) {
              console.log(error.request);
            } else {
              console.log('Error', error.message);
            }
        })
      }
    },
  },
  created() {
    this.member_role = sessionStorage.getItem("member_role", "0")
    this.getCardAgencyInfo()
  },

};
</script>

<style  scoped>

</style>